package com.demo.service.impl;


import java.util.Date;
import java.util.List;

import com.demo.entity.enums.ResponseCodeEnum;
import com.demo.entity.enums.UserRoleTypeEnum;
import com.demo.entity.po.CounselorInfo;

import com.demo.entity.po.UserInfo;
import com.demo.entity.query.CounselorInfoQuery;

import com.demo.entity.query.UserInfoQuery;
import com.demo.entity.vo.PaginationResultVO;

import com.demo.exception.BusinessException;
import com.demo.mappers.UserInfoMapper;
import com.demo.service.CounselorInfoService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.demo.entity.query.SimplePage;

import com.demo.mappers.CounselorInfoMapper;

import com.demo.entity.enums.PageSize;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description:咨询师表serviceImpl
 * @Date:2025-03-04
 * @author：author
*/
@Service("counselorInfoService")
public class CounselorInfoServiceImpl implements CounselorInfoService{

	@Resource
	private CounselorInfoMapper<CounselorInfo,CounselorInfoQuery> counselorInfoMapper;

	@Resource
	private UserInfoMapper<UserInfo, UserInfoQuery> userInfoMapper;

	/**
	 * 根据条件查询列表
	 */
	public List<CounselorInfo> findListByParam(CounselorInfoQuery query) { 
		return this.counselorInfoMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(CounselorInfoQuery query){
		return this.counselorInfoMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<CounselorInfo> findListByPage(CounselorInfoQuery  query) { 
		int count = this.findCountByParam(query);
		int pageSize = query.getPageSize()==null?PageSize.SIZE15.getSize():query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(),count,pageSize);
		query.setSimplePage(page);
		List<CounselorInfo> list = this.findListByParam(query);
		return new PaginationResultVO(count,page.getPageSize(),page.getPageNo(),page.getPageTotal(),list);
	}

	/**
	 * 新增
	 */
	public Integer add(CounselorInfo bean) { 
		return this.counselorInfoMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<CounselorInfo> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.counselorInfoMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	public Integer addOrUpdateBatch(List<CounselorInfo> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.counselorInfoMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据CounselorId查询
	 */
	public CounselorInfo getCounselorInfoByCounselorId(Long counselorId) { 
		return this.counselorInfoMapper.selectByCounselorId(counselorId);
	}

	/**
	 * 根据CounselorId更新
	 */
	public Integer updateCounselorInfoByCounselorId(CounselorInfo bean, Long counselorId) { 
		return this.counselorInfoMapper.updateByCounselorId(bean,counselorId);
	}

	/**
	 * 根据CounselorId删除
	 */

	public Integer deleteCounselorInfoByCounselorId(Long counselorId) { 
		// TODO 用户已经有预约不允许删除
		return this.counselorInfoMapper.deleteByCounselorId(counselorId);
	}

	/**
	 * 根据UserId查询
	 */
	public CounselorInfo getCounselorInfoByUserId(Long userId) { 
		return this.counselorInfoMapper.selectByUserId(userId);
	}

	/**
	 * 根据UserId更新
	 */
	public Integer updateCounselorInfoByUserId(CounselorInfo bean, Long userId) { 
		return this.counselorInfoMapper.updateByUserId(bean,userId);
	}

	/**
	 * 根据UserId删除
	 */
	public Integer deleteCounselorInfoByUserId(Long userId) { 
		return this.counselorInfoMapper.deleteByUserId(userId);
	}


	/**
	 * 保存咨询师信息
	 * @param counselorInfo
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveInfo(CounselorInfo counselorInfo) {
		UserInfo userInfoDB = userInfoMapper.selectByUserId(counselorInfo.getUserId());
		if (userInfoDB == null) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		Date date = new Date();
		if (counselorInfo.getCounselorId() == null) {// 新增
			counselorInfo.setCreateTime(date);
			counselorInfo.setUpdateTime(date);
			counselorInfoMapper.insert(counselorInfo);
			UserInfo updateInfo = new UserInfo();
			updateInfo.setRole(UserRoleTypeEnum.COUNSELOR.getType());
			userInfoMapper.updateByUserId(updateInfo,counselorInfo.getUserId());
		}else {
			counselorInfo.setUpdateTime(date);
			counselorInfoMapper.insertOrUpdate(counselorInfo);
		}
	}


	@Override
	public void updateStatus(Long counselorId, Integer status) {
		CounselorInfo counselorInfoDB = counselorInfoMapper.selectByCounselorId(counselorId);
		if (counselorInfoDB == null) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		CounselorInfo updateInfo = new CounselorInfo();
		updateInfo.setStatus(status);
		counselorInfoMapper.updateByCounselorId(updateInfo,counselorId);
	}
}